package com.xbongbong.saas.help;

import com.alibaba.fastjson.JSONArray;
import com.xbongbong.paas.manager.feign.ChartResultFeignClient;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.statistic.enums.SystemCodeEnum;
import com.xbongbong.pro.statistic.result.pojo.ChartResultPojo;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.service.UserTeamService;
import com.xbongbong.sys.domain.entity.UserEntity;
import com.xbongbong.sys.model.UserModel;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.annotation.Resource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

/**
 * @author feng.zheng
 * @version v1.0
 * @date 2019/3/7 10:57
 * @since v1.0
 */
@Component
public class BasicHelper {
    @Resource
    private UserModel userModel;
    @Resource
    private UserTeamService userTeamService;
    @Resource
    private ChartResultFeignClient chartResultFeignClient;

    /**
     * 封装es查询时间
     *
     * @param time 时间
     * @author feng.zheng
     * @date 2019/3/7 11:08
     */
    public static List<Object> timeStrToValueList(JSONArray time) {
        List<Object> valueList = new ArrayList<>();
        if (time != null && !time.isEmpty()) {
            String timeStartStr =  time.getString(BasicConstant.ZERO);
            Long theTimeStart = DateTimeUtil.getInt(timeStartStr, DateUtil.SDFDate);
            valueList.add(theTimeStart);
            if (time.size() > 1) {
                String timeEndStr = time.getString(BasicConstant.ONE);
                Long theTimeEnd = DateTimeUtil.getInt(timeEndStr + " 24:00", DateUtil.SDFYMDHM);
                valueList.add(theTimeEnd);
            }
        }
        return valueList;
    }

    /**
     * 添加、删除负责人权限：老板、超管、负责人自己（可以删除别人和自己）；
     * 添加、删除协同人权限：老板、超管、负责人；
     *
     * @param userId     操作人员
     * @param dataId 数据id
     * @param corpid     公司
     * @param buinessType 业务类型
     * @return true/false 有权限/无权限
     */
    public boolean checkPermission(String userId, Long dataId, String corpid, Integer buinessType) {
        UserEntity user = userModel.getByKey(userId, corpid);
        if (user == null) {
            return false;
        }
        // 判断是否是老板或超管
        if (userModel.isBoss(user)) {
            return true;
        }
        return userTeamService.getUserTeamList4CheckBeyondZero(corpid, userId, dataId, buinessType, BasicConstant.ONE);
    }

    /**
     * 从统计项目得到指标统计结果（区间总数值）
     * @param chartResultPojo 统计参数pojo
     * @param systemCodeEnum 系统指标枚举
     * @return java.lang.String
     * @author zcp
     * @date 2019/5/24 19:45
     * @since v1.0
     * @version v1.0
     */
    public String getChartResultValue(ChartResultPojo chartResultPojo, SystemCodeEnum systemCodeEnum) {
        chartResultPojo.setSystemCodeEnum(systemCodeEnum);
        Locale locale = LocaleContextHolder.getLocale();
        String lang = locale.toString();
        Object obj = chartResultFeignClient.value(chartResultPojo, lang);
        String valueStr = (String) ((XbbResponse) obj).getResult();
        return valueStr;
    }

    /**
     * 得到double类型的统计结果
     * @param chartResultPojo 参数pojo
     * @param systemCodeEnum 系统枚举
     * @return java.lang.Double
     * @author zcp
     * @date 2019/5/23 10:38
     * @since v1.0
     * @version v1.0
     */
    public Double getChartResultValue4Double(ChartResultPojo chartResultPojo, SystemCodeEnum systemCodeEnum) {
        String valueStr = getChartResultValue(chartResultPojo, systemCodeEnum);
        if (StringUtil.isEmpty(valueStr)){
            return 0D;
        } else {
            //double的需要把逗号去除
            valueStr = valueStr.replace(",", "");
        }
        return Double.valueOf(valueStr);
    }
}
